<template>
  <!-- 虫情自动监测 -->
  <baseslot>
    <template v-slot:content>
      <div class="pest-main">
        <div class="zh-pest-side">
          <myside @peststation="nowpestmessage" />
        </div>
        <div class="zh-pest-main">
          <div class="pest-main-topsearch">
            <el-date-picker
              v-model="searchdateList"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            />
            <div class="pest-search-but" @click="getimagemessage(insectDeviceId)"></div>
          </div>
          <div class="pest-main-center">
            <div class="main-center-left">
              <div
                class="center-left-title-one"
              >图像编号: {{ imageList[imageselectindex].picture.pictureName }}</div>
              <div class="title-one-item">采集时间: {{ imageList[imageselectindex].collectionTime }}</div>
              <div class="title-one-item">昆虫数量: {{ pestnum }}只</div>
              <div class="title-one-item">经度: {{ nowstationmessage.latitude }}</div>
              <div class="title-one-item">纬度: {{ nowstationmessage.longitude }}</div>
              <div class="center-left-title-two">各昆虫数量</div>
              <div class="pest-menu">
                <div
                  v-for="(
                    item, index
                  ) in nowimagemes.insectCollectionResultList"
                  :key="index"
                  class="pest-menu-item"
                >{{ item.insectName }}: {{ item.insectNum }}只</div>
              </div>
            </div>
            <div class="main-center-right">
              <div v-if="!imageList[imageselectindex].picture.pictureData">暂无图片</div>
              <img
                v-if="imageList[imageselectindex].picture.pictureData"
                :src="
                  'data:image/png;base64,' +
                    imageList[imageselectindex].picture.pictureData
                "
                width="780px"
                height="380px"
              />
            </div>
          </div>
          <div class="pest-main-bottom">
            <div class="pest-bottom-left" @click="minpagesize"></div>
            <div class="pest-bottom-center">
              <div
                v-for="(itemtwo, indextwo) in imageList"
                :key="indextwo"
                class="bottom-center-item"
                :class="
                  imageselectindex === indextwo &&
                    itemtwo.picture.pictureData.length > 0
                    ? 'bottom-center-item-select'
                    : ''
                "
                @click="setimageindex(itemtwo, indextwo)"
              >
                <img
                  v-if="itemtwo.picture.pictureData.length > 0"
                  class="bottom-center-item-img"
                  :src="'data:image/png;base64,' + itemtwo.picture.pictureData"
                />
              </div>
            </div>
            <div class="pest-bottom-right" @click="addpagesize"></div>
          </div>
        </div>
      </div>
    </template>
  </baseslot>
</template>
<script>
import nowpagereq from "@/api/digital-production/pest-management/insect-alert";
import baseslot from "@/components/BaseMainSlot/baseone.vue";
import myside from "./components/side.vue";
export default {
  name: "AutomaticInsectMonitoring",
  components: {
    baseslot,
    myside
  },
  data() {
    return {
      showImageList: true,
      pestnum: 0,
      currentpage: 1,
      currentsize: 3,
      nowstationmessage: {
        insectCollectionResultList: [],
        latitude: "",
        longitude: ""
      },
      insectDeviceId: "",
      imageselectindex: 0,
      nowimagemes: {},
      imageList: [
        {
          id: 1,
          picture: {
            pictureName: "",
            pictureData: ""
          }
        }
      ],
      searchdateList: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },
  mounted() {},
  methods: {
    nowpestmessage(e) {
      this.showImageList = true;
      this.imageselectindex = 0;
      this.imageList = [
        {
          id: 999,
          picture: {
            pictureName: "",
            pictureData: ""
          }
        }
      ];
      this.currentpag = 1;
      // console.log(e);
      if (e !== undefined) {
        this.nowstationmessage = e;
      }
      this.insectDeviceId = e?.insectDeviceId;
      this.getimagemessage(e?.insectDeviceId);
    },
    // 下一页
    addpagesize() {
      if (this.showImageList) {
        this.imageselectindex = 0;
        this.currentpage = this.currentpage + 1;
        this.getimagemessage(this.insectDeviceId);
        this.$message.success("下一页获取成功,当前页数为" + this.currentpage);
      } else {
        this.$message.success("已经是最后一页了");
      }
    },
    // 上一页
    minpagesize() {
      this.showImageList = true;
      this.imageselectindex = 0;
      this.currentpage = this.currentpage - 1;
      if (this.currentpage < 1) {
        this.currentpage = 1;
        this.$message.warning("页数已为1");
        return;
      }
      this.getimagemessage(this.insectDeviceId);
      this.$message.success("上一页获取成功，当前页数为" + this.currentpage);
    },
    // 获取图片信息
    getimagemessage(id) {
      this.imageList = [
        {
          id: 1,
          picture: {
            pictureName: "",
            pictureData: ""
          }
        }
      ];
      let param = {
        page: this.currentpage,
        size: this.currentsize,
        entity: {
          insectDeviceId: id,
          endTime: "",
          startTime: ""
        }
      };
      // 默认获取当前月数据
      let date = this.dateToFormatthree(new Date());
      param.entity.startTime = `${date}-01 00:00:01`;
      param.entity.endTime = `${date}-30 23:59:59`;
      if (this.searchdateList[0]) {
        param.entity.startTime = this.dateToFormat(this.searchdateList[0]);
        param.entity.endTime = this.dateToFormat(this.searchdateList[1]);
      }
      nowpagereq.getpestimage(param).then(res => {
        if (this.currentpage >= res.data.totalPages) {
          this.showImageList = false;
        }
        this.imageList = res.data.content;
      });
    },
    setimageindex(e, i) {
      this.nowimagemes = e;
      this.pestnum = 0;
      if (e.insectCollectionResultList.length > 0) {
        e.insectCollectionResultList.forEach(item => {
          this.pestnum += item.insectNum;
        });
      }
      this.imageselectindex = i;
    }
  }
};
</script>
<style lang="scss" scoped>
.pest-main {
  width: 100%;
  height: 99%;
  display: flex;
}
.zh-pest-side {
  width: 230px;
  height: calc(100% - 0px);
  // border:aqua 1px solid;
  // overflow: auto;
  background-color: rgba(0, 72, 90, 0.7);
}
.zh-pest-main {
  width: calc(100% - 245px);
  margin-left: 15px;
  height: 100%;
  .pest-main-topsearch {
    width: 100%;
    height: 50px;
    display: flex;
    .pest-search-but {
      cursor: pointer;
      width: 80px;
      height: 40px;
      background-image: url("~@/assets/action-bar-icon/search.png");
      background-size: 80px 40px;
      background-repeat: no-repeat;
      background-position: center;
      margin-left: 10px;
    }
  }
  .pest-main-center {
    width: 100%;
    height: 400px;
    display: flex;
    background-color: rgba(0, 110, 154, 0.1);
    overflow: hidden;
    .main-center-left {
      width: calc(100% - 800px);
      height: 400px;
      .center-left-title-one {
        width: 90%;
        height: 30px;
        line-height: 30px;
        text-align: center;
        font-size: 16px;
        border-bottom: 1px dashed #fff;
        margin: 5px auto 10px;
        white-space: nowrap;
      }
      .title-one-item {
        width: 90%;
        height: 30px;
        line-height: 30px;
        text-align: center;
        margin: 5px auto 10px;
        font-size: 16px;
        border: 1px solid #7bdcf6;
        font-weight: 700;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .center-left-title-two {
        width: 90%;
        height: 30px;
        line-height: 30px;
        text-align: center;
        margin: 20px auto 10px;
        font-size: 16px;
        border: 1px solid #7bdcf6;
        font-weight: 700;
        background-color: rgba(0, 110, 145, 0.4);
      }
      .pest-menu {
        width: 90%;
        text-align: center;
        margin: 20px auto 10px;
        font-size: 16px;
        overflow: auto;
        height: 180px;
        // border: chartreuse 1px solid;
        .pest-menu-item {
          width: 100%;
          text-align: center;
          height: 30px;
          line-height: 30px;
          border: 1px solid #7bdcf6;
          margin-bottom: 10px;
        }
      }
    }
    .main-center-right {
      margin-right: 7px;
      width: 800px;
      height: 400px;
      border: 8px double #fff;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .pest-main-bottom {
    width: 97.5%;
    margin-left: 2%;
    margin-top: 10px;
    height: calc(100% - 470px);
    min-height: 100px;
    overflow: auto;
    border: solid 1px #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .pest-bottom-left {
      width: 60px;
      height: 60px;
      margin-left: 10px;
      background-image: url("~@/assets/sxw-pest/left.png");
      background-repeat: no-repeat;
      background-size: 60px;
      cursor: pointer;
    }
    .pest-bottom-center {
      width: calc(100% - 160px);
      height: 180px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .bottom-center-item {
        width: 24%;
        height: 180px;
        display: flex;
        align-items: center;
        cursor: pointer;
        .bottom-center-item-img {
          width: 100%;
          height: 160px;
          border: teal 1px solid;
        }
      }
      .bottom-center-item-select {
        border: #00c69e 4px solid;
      }
    }
    .pest-bottom-right {
      width: 60px;
      height: 60px;
      margin-right: 10px;
      background-image: url("~@/assets/sxw-pest/right.png");
      background-repeat: no-repeat;
      background-size: 60px;
      cursor: pointer;
    }
  }
}
</style>
